package cn.com.dashihui.api.radisysnc.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.PropKit;

import cn.com.dashihui.api.radisysnc.inter.IRequest;
import cn.com.dashihui.api.radisysnc.request.RequestQueue;
/**
 * ***********************************************
　 * <p>类名: RequestProcessorThreadPool</p> 
　 * <p>描述: 请求处理线程池，初始化队列数及每个队列最多能处理的数量</p> 
　 * @author niufc 
　 * @date 2019年8月19日 
  * ************************************************
 */
public class RequestProcessorThreadPool {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static final int blockingQueueNum = PropKit.get("request.blockingqueue.number")==null?10:Integer.valueOf(PropKit.get("request.blockingqueue.number").toString());
    private static final int queueDataNum = PropKit.get("request.everyqueue.data.maxlength")==null?100:Integer.valueOf(PropKit.get("request.everyqueue.data.maxlength").toString());
    
    //创建有固定大小的线程池
    private ExecutorService threadPool = Executors.newFixedThreadPool(blockingQueueNum);
    private RequestProcessorThreadPool(){
        for(int i=0;i<blockingQueueNum;i++){//初始化队列
            ArrayBlockingQueue<IRequest> queue = new ArrayBlockingQueue<IRequest>(queueDataNum);//每个队列中放100条数据
            RequestQueue.getInstance().addQueue(queue);
            threadPool.submit(new RequestProcessorThread(queue));//把每个queue交个线程去处理，线程会处理每个queue中的数据
        }
    }
    public static class Singleton{
       private static RequestProcessorThreadPool instance;
       static{
           instance = new RequestProcessorThreadPool();
       }
       public static RequestProcessorThreadPool getInstance(){
           return instance;
       }
    }
    public static RequestProcessorThreadPool getInstance(){
        return Singleton.getInstance();
    }
    /**
     * 初始化线程池
     */
    public static void init(){
        getInstance();
    }

}
